package com.zensun.common.utils;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.zensun.common.exception.CustomException;

import java.util.Collection;
import java.util.regex.Pattern;

/**
 * 数据校验
 */
public abstract class Assert {

    /**
     * 校验字符串
     *
     * @param str
     * @param message
     */
    public static void isBlank(String str, String message) {
        if (CharSequenceUtil.isBlank(str)) {
            throw new CustomException(message);
        }
    }

    /**
     * 除了字符串的校验
     *
     * @param object
     * @param message
     */
    public static void isNull(Object object, String message) {
        if (object == null) {
            throw new CustomException(message);
        }
    }

    /**
     * 校验集合
     *
     * @param collection
     * @param message
     */
    public static void isEmpty(Collection collection, String message) {
        if (CollUtil.isEmpty(collection)) {
            throw new CustomException(message);
        }
    }

    /**
     * 校验集合
     *
     * @param collection
     * @param message
     */
    public static void isNotEmpty(Collection collection, String message) {
        if (!CollUtil.isEmpty(collection)) {
            throw new CustomException(message);
        }
    }

    /**
     * 校验集合
     *
     * @param collection1 1包含2
     * @param collection2
     * @param message
     */
    public static void isContainsAll(Collection collection1, Collection collection2, String message) {
        if (!collection1.containsAll(collection2)) {
            throw new CustomException(message);
        }
    }

    /**
     * boolean校验
     *
     * @param object
     * @param message
     */
    public static void isTrue(Boolean object, String message) {
        if (object) {
            throw new CustomException(message);
        }
    }

    /**
     * boolean校验
     *
     * @param object
     * @param message
     */
    public static void isNotTrue(Boolean object, String message) {
        if (!object) {
            throw new CustomException(message);
        }
    }

    /**
     * 手机号格式校验
     *
     * @param phone
     * @return
     */
    public static void isPhone(String phone) {
        if (!Pattern.matches("^1[0-9]{10}$", phone.trim())) {
            throw new CustomException("手机号格式不正确");
        }
    }


    /**
     * 判断数量是否大于0
     *
     * @param count
     * @param message
     */
    public static void countGtZero(int count, String message) {
        if (count > 0) {
            throw new CustomException(message);
        }
    }

    /**
     * 判断数量是否小于0
     *
     * @param count
     * @param message
     */
    public static void countLtZero(int count, String message) {
        if (count <= 0) {
            throw new CustomException(message);
        }
    }

    /**
     * 校验密码
     *
     * @param password
     * @param message
     */
    public static void isPassword(String password, String message) {
        String re = "^([0-9a-zA-Z]|[!\\@$#_~\\?&\\%*]){6,20}$";
        if (!Pattern.matches(re, password)) {
            throw new CustomException(message);
        }
    }
}
